package test.app.core.demo.controller;

import static org.junit.Assert.assertTrue;

import java.util.List;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.frame.app.core.demo.model.DemoClassroom;
import com.frame.app.core.demo.model.DemoStudent;


@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration(locations = { "classpath*:/WEB-INF/conf/applicationContext-*.xml" })
@ContextConfiguration(locations = { "file:WebRoot/WEB-INF/conf/applicationContext-*.xml" })
public class IndexController extends AbstractTransactionalJUnit4SpringContextTests{
	
    @Override  
    @Resource(name = "dataSource")  
    //有多个数据源时就必须设置@Resource
    public void setDataSource(DataSource dataSource) {  
        // TODO Auto-generated method stub  
        super.setDataSource(dataSource);  
    }
	
    @BeforeClass
    public static void beforeClass() throws Exception {
        System.out.println("##################junit测试开始##################");
    }
    
	@Test
	//不回滚
	@Rollback(false)
	//不进事务
	//@NotTransactional
	public void index() throws Exception {
		//自定义方法
		//DemoStudent student = new DemoStudent();
		//student.fetchMapperClass().truncate();
		
		//如果要查询必须设置页数
		DemoStudent student1 = new DemoStudent();
		student1.setClassroomId(1);
		student1.setName(String.valueOf((int)(Math.random()*100)));
		student1.save();
		assertTrue(true);
		
		
		//下面是自动外键的设置方法
		DemoStudent student2 = new DemoStudent();
		student2.setId(1);
		student2.foreignKey("classroomId", DemoClassroom.class);
		DemoStudent result = student2.load();
		System.out.println("###################" + result.getName());
		System.out.println("###################" + result.getDemoClassroom().getName());
		assertTrue(true);	
		
		
		//查询列表
		DemoStudent student3 = new DemoStudent();
		student3.setPage(1);
		student3.setPerPageSize(50);
		List<DemoStudent> array = student3.find();
		System.out.println(array);
		System.out.println(array.size());
		assertTrue(true);	
		
		//查询所有
		DemoStudent condition = new DemoStudent();
		List<DemoStudent> all = condition.fetchMapperClass().findAll();
		System.out.println(all);
		System.out.println(all.size());
		assertTrue(true);	
		
//		MockHttpServletRequest request = new MockHttpServletRequest();
//        MockHttpServletResponse response = new MockHttpServletResponse();
//         
//        request.setRequestURI("/index");
//        request.setMethod("GET");
//        
//        try{
//            final ModelAndView mav = this.excuteAction(request, response);
//            System.out.println(mav.getViewName());
//            Object msg = mav.getModelMap().get("demos");
//            System.out.println(" = " + msg);
//        }catch (Exception e){
//            e.printStackTrace();
//        }
	}
	
    @AfterClass
    public static void afterClass() throws Exception {
        System.out.println("##################junit测试结束##################");
    }
}
